﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenESSOConfigurator.BusinessLayer;
using OpenESSOConfigurator.BusinessLayer.Atoms;
using OpenESSOConfigurator.BusinessLayer.Stores;
using System.Windows.Forms;
using System.ComponentModel;

namespace OpenESSOConfigurator.PresentationLayer.PresentationModels
{
    public class ItemTreePresentationModel: PresentationModel
    {
        private static ItemTreePresentationModel instance;
        private OpenESSOConfiguration _openESSOConfiguration;
        private WinApplication _selectedWinApplication;
        private WinForm _selectedWinForm;
        private WebForm _selectedWebForm;
        private WebApplication _selectedWebApplication;
        private CredentialSharingGroup _selectedCSG;
        private PasswordPolicy _selectedPasswordPolicy;
        private ConfigurationStore _selectedConfigurationStore;

        

        private ItemTreePresentationModel()
        {
            eventBroker = new OpenESSOConfigurator.PresentationLayer.EventBroker.EventBroker();
            views = new List<IView>();
        }

        public static ItemTreePresentationModel Instance
        {
            get 
            {
                if (instance == null) instance = new ItemTreePresentationModel();
                return instance;
            }
        }

        public OpenESSOConfiguration OpenESSOConfiguration
        {
            set 
            { 
                _openESSOConfiguration = value;
                this.FireEvent("topic://ItemTreePresentationModel/ConfigurationChanged", _openESSOConfiguration, null);
            }
            get { return _openESSOConfiguration; }
        }

        public WinApplication SelectedWinApplication
        {
            set
            {
                //int i = OpenESSOConfiguration.Applications.WinApplications.FindIndex(0, delegate(WinApplication winapp) { return winapp.Id == value.Id; });
                //OpenESSOConfiguration.Applications.WinApplications[i] = value;
                //this.FireEvent("topic://ItemTreePresentationModel/ConfigurationChanged", _openESSOConfiguration, null);
                _selectedWinApplication = value;
                this.FireEvent("topic://ItemTreePresentationModel/SelectedWinApplicationChanged", _selectedWinApplication, null);
            }
            get { return _selectedWinApplication; }
        }

        public WinForm SelectedWinForm
        {
            set
            {
               // int i = OpenESSOConfiguration.Applications.WinApplications.FindIndex(0, delegate(WinApplication winapp) { return winapp.Id == value.Id; });
               // OpenESSOConfiguration.Applications.WinApplications[i] = value;
               // this.FireEvent("topic://ItemTreePresentationModel/ConfigurationChanged", _openESSOConfiguration, null);
                _selectedWinForm = value;
                this.FireEvent("topic://ItemTreePresentationModel/SelectedWinFormChanged", _selectedWinForm, null);
            }
            get { return _selectedWinForm; }
        }

        public WebForm SelectedWebForm
        {
            set
            {
                // int i = OpenESSOConfiguration.Applications.WinApplications.FindIndex(0, delegate(WinApplication winapp) { return winapp.Id == value.Id; });
                // OpenESSOConfiguration.Applications.WinApplications[i] = value;
                // this.FireEvent("topic://ItemTreePresentationModel/ConfigurationChanged", _openESSOConfiguration, null);
                _selectedWebForm = value;
                this.FireEvent("topic://ItemTreePresentationModel/SelectedWebFormChanged", _selectedWebForm, null);
            }
            get { return _selectedWebForm; }
        }

        public WebApplication SelectedWebApplication
        {
            set
            {
                int i = OpenESSOConfiguration.Applications.WebApplications.FindIndex(0, delegate(WebApplication webapp) { return webapp.Id == value.Id; });
                OpenESSOConfiguration.Applications.WebApplications[i] = value;
                this.FireEvent("topic://ItemTreePresentationModel/ConfigurationChanged", _openESSOConfiguration, null);
                _selectedWebApplication = value;
                this.FireEvent("topic://ItemTreePresentationModel/SelectedWebApplicationChanged", _selectedWebApplication, null);
            }
            get { return _selectedWebApplication; }
        }
        public CredentialSharingGroup SelectedCSG
        {
            set
            {
                //int i = OpenESSOConfiguration.CredentialSharingGroups.FindIndex(0, delegate(CredentialSharingGroup csg) { return csg.Id == value.Id; });
                //OpenESSOConfiguration.CredentialSharingGroups[i] = value;
                //this.FireEvent("topic://ItemTreePresentationModel/ConfigurationChanged", _openESSOConfiguration, null);
                _selectedCSG = value;
                this.FireEvent("topic://ItemTreePresentationModel/SelectedCSGChanged", _selectedCSG, null);
            }
            get { return _selectedCSG; }
        }
        public PasswordPolicy SelectedPasswordPolicy
        {
            set
            {
                //int i = OpenESSOConfiguration.PasswordPolicies.FindIndex(0, delegate(PasswordPolicy pwdp) { return pwdp.Id == value.Id; });
                //OpenESSOConfiguration.PasswordPolicies[i] = value;
                //this.FireEvent("topic://ItemTreePresentationModel/ConfigurationChanged", _openESSOConfiguration, null);
                _selectedPasswordPolicy = value;
                this.FireEvent("topic://ItemTreePresentationModel/SelectedPasswordPolicyChanged", _selectedPasswordPolicy, null);
            }
            get { return _selectedPasswordPolicy; }
        }

        public ConfigurationStore SelectedConfigurationStore
        {
            set
            {
                _selectedConfigurationStore = value;
                this.FireEvent("topic://ItemTreePresentationModel/SelectedConfigurationStoreChanged", _selectedConfigurationStore, null);
            }
            get { return _selectedConfigurationStore; }
        }
             

    }
}
